//以对话框形式打开一个新URL，该URL所指的页面应包含以下限定：
//1、要求内置页必须有两个函数
//      getReturnText();
//      getReturnValue();
//2、应能够处理输入的QueryString变量in
//该函数的功能为，打开一个新URL页，并根据页面上述函数的返回值自动填充指定文本域的值
function jDialogWithReturn(url, title, width, height, inVal, outValueFld, outTextFld) {
    if (inVal.indexOf("#") == 0) {
        inVal = $(inVal).val();
    }
    if (typeof (outTextFld) == "string") outTextFld = $("#" + outTextFld);
    if (typeof (outValueFld) == "string") outValueFld = $("#" + outValueFld);
    if (window.top != window && window.top.jDialogWithReturn) { window.top.jDialogWithReturn(url, title, width, height, inVal, outValueFld, outTextFld); return false; }
    if ($("#__jDialogWithReturn").length == 0) {
        $(document.body).append("<div id='__jDialogWithReturn' ><iframe id='__jDialogWithReturn_Iframe' border='0' src='about:blank' width='100%' height='100%' ></iframe></div>");
    }
    if (url.indexOf("?") == -1)
        url += '?in=' + inVal;
    else
        url += '&in=' + inVal;
    $('#__jDialogWithReturn_Iframe').attr("src", "about:blank");
    $('#__jDialogWithReturn_Iframe').attr("src", url);
//    $('#__jDialogWithReturn_Iframe').attr("width", width - 20);
//    $('#__jDialogWithReturn_Iframe').attr("height", height-20);
    $("#__jDialogWithReturn").dialog({ width: width, height: height, title: title, modal: true, buttons: [
                { text: "确定", iconCls: "icon-ok", handler: function () {
                    var win = document.getElementById("__jDialogWithReturn_Iframe").contentWindow;
                    if (win && win.getReturnValue) {
                        outValueFld.val(win.getReturnValue());
                        outTextFld.val(win.getReturnText());
                    }
                    $("#__jDialogWithReturn").dialog("close");

                }
                },
                { text: "取消", iconCls: "icon-cancel", handler: function () { $("#__jDialogWithReturn").dialog("close"); } }

            ]
    });
    $("#__jDialogWithReturn").dialog("open");
    return false;
}
var __FFunc = function () { };
function jDialogRegisterFinishedFunction(func) {
    __FFunc = func;
}
function jDialogWithoutReturn(url, title, width, height, finishedFunc) {
    if(!finishedFunc)finishedFunc = __FFunc;
    if (window.parent && window.parent != window && window.parent.jDialogWithoutReturn) { window.parent.jDialogWithoutReturn(url, title, width, height, finishedFunc); return false; }
    if (title == null) title = "对话框";
    if (width == null) width = 700;
    if (height == null) height = 480;
    if (height+40 > $(document.body).height()) height = $(document.body).height()-40;
    if ($("#__jDialogWithOutReturn").length == 0) {
        $(document.body).append("<div id='__jDialogWithOutReturn' style='overflow:hidden;' ><div style='width:100%;height:100%;position:relative;' ><iframe id='__jDialogWithOutReturn_Iframe' frameborder=no border='0' src='about:blank' width='100%' height='100%' ></iframe></div><div id='__jDialogWithOutReturnLoading' style='height:100%;'></div></div>");
        $('#__jDialogWithOutReturn_Iframe').load(function () { $('#__jDialogWithOutReturnLoading').parent().find("div").css("top", "0"); });
        //$("<iframe id=\"__jDialogWithOutReturn_IframeBorder\" ></iframe>").appendTo($(document.body));
    }
    $('#__jDialogWithOutReturnLoading').parent().find("div").css("top", "-100%")
    $('#__jDialogWithOutReturn_Iframe').attr("src", url);
    if (!finishedFunc && $("#__jDialogWithOutReturn").hasClass("panel-body")) finishedFunc = $("#__jDialogWithOutReturn").dialog("options").onClose;
    if (!finishedFunc) finishedFunc = function () { window.location.href = window.location.href; };
    $("#__jDialogWithOutReturn").dialog({ width: width, height: height, title: title, modal: true, onClose: function () {
        //$("#__jDialogWithOutReturn_IframeBorder").css("visibility", "hidden");
        if (finishedFunc)try{ finishedFunc();}catch(e){}
    }//, onMove: function (left, top) { var p = $("#__jDialogWithOutReturn").parent(); $("#__jDialogWithOutReturn_IframeBorder").css("top", p.css("top")).css("left", p.css("left")); }
    });
    $("#__jDialogWithOutReturn").dialog("open");
    var p = $("#__jDialogWithOutReturn").parent();
    //$("#__jDialogWithOutReturn_IframeBorder").attr("border",0).css("position", p.css("position")).css("top", p.css("top")).css("left", p.css("left")).css("width", parseFloat(p.css("width"))+10).css("height", parseFloat(p.css("height"))+10).css("z-index", parseInt(p.css("z-index")) - 1).css("visibility", "visible");
    return false;
}
function closeDialog(fromWindow) {
    if (!fromWindow) return false;
    if ($('#__jDialogWithOutReturn_Iframe').length == 0 || $('#__jDialogWithOutReturn_Iframe')[0].contentWindow != fromWindow) {
        if (window.parent != window && window.parent.closeDialog) return window.parent.closeDialog(fromWindow);
        else return false;
    }
    $("#__jDialogWithOutReturn").dialog("close");
    return true;
}

var easyuiPanelOnMove = function (left, top) {
    var parentObj = $(this).panel('panel').parent();
    var ln = left, tn = top;
    if (left < 0) ln = 1;
    if (top < 0) tn = 1;
    var width = $(this).panel('options').width;
    var height = $(this).panel('options').height;
    var right = left + width;
    var buttom = top + height;
    var parentWidth = parentObj.width();
    var parentHeight = parentObj.height();
    if (parentObj.css("overflow") == "hidden") {
        if (parentWidth < width && left > parentWidth - width) ln = parentWidth - width
        if (parentHeight > $(this).parent().height() && top > parentHeight - $(this).parent().height())
            tn = parentHeight - $(this).parent().height()
    }
    if (tn != top || ln != left)
        $(this).panel("move", {left:ln,top:tn});
};
if ($.fn.panel) $.fn.panel.defaults.onMove = easyuiPanelOnMove;
//$.fn.window.defaults.onMove = easyuiPanelOnMove;
//$.fn.dialog.defaults.onMove = easyuiPanelOnMove;

function mergeCells(tab, data, mergeFields) {
    var fields = mergeFields;
    if (typeof (fields) == "string")
        fields = eval("(" + mergeFields + ")");
    if (typeof (data) == "string")
        data = eval("(" + data + ")");
    for (var field in fields) {
        var indx = 0;
        for (var i = 0; i < data.total; i++) {
            if (data.rows[i] != undefined && data.rows[i][fields[field]] != data.rows[indx][fields[field]]) {
                if (i > indx + 1)
                    $(tab).datagrid('mergeCells', {
                        index: indx,
                        field: fields[field],
                        rowspan: i - indx
                    });
                indx = i;
            }
        }
        if (indx + 1 < data.total)
            $(tab).datagrid('mergeCells', {
                index: indx,
                field: fields[field],
                rowspan: data.total - indx
            });
    }
}
function InitStatistics(datagrid, url, searchDiv, searchBtn) {
    $(searchBtn).click(function () {
        var param = {};
        if ($(searchDiv).form("validate")) {
            $(searchDiv + " select").each(function () {
                var id = $(this).attr("id");
                var ids = id.substring(id.indexOf("DDL_")); //.split("_");
                if ($(this).hasClass("easyui-combobox"))
                    param[ids] = $(this).combobox("getValue");
                else param[ids] = $(this).val();
            });
            param["T"] = new Date().getTime();
            var ps = $.QueryString();
            for (var i = 0; i < ps.length; i++) {
                var pv = ps[i].split("=");
                if (pv.length > 1) {
                    for (var k in param) {
                        if (k == pv[0]) { pv = null; break; }
                    }
                }
                if (pv) param[pv[0]] = pv[1];
            }
        } else return false;
        Application.queryData(param, function (data, scope) {
            data = eval("(" + data + ")");
            $(datagrid).datagrid({ data: data.data,
                columns: data.columns
            });
            //mergeCells(this, data, data.mergeFields);
        }, this, "GET", url);
        return false;
    }).click();
}
